<?php
declare (strict_types = 1);

namespace app\common\model;

use think\facade\Db;
use think\Model;
use think\model\concern\SoftDelete;
use app\common\model\StudyStudentClass as M;
class StudyClassSign extends Model
{
    use SoftDelete;
     protected $deleteTime = false;
    // 获取列表
    public static function getList()
    {
        $where = [];
        $limit = input('get.limit');
        
               //按打卡时间查找
//               $start = input("get.add_time-start");
//               $end = input("get.add_time-end");
//               if ($start && $end) {
//                   $where[]=["add_time","between",[$start,date("Y-m-d",strtotime("$end +1 day"))]];
//               }
//        $list = self::order('id','desc')->with('getClass')->where($where)->paginate($limit)->each(function (&$item){
//            $item['add_time'] = date('Y-m-d H:i:s',$item['add_time']);
//        });

        $list = M::order('id','desc')->where($where)->paginate($limit)->each(function (&$item){
            //查询这个班级的所有学生
            $stuId = StudyStudentList::where('class_id',$item['id'])->column('id');

            //查询今日打卡的学生
            $countData = Db::table('bl_student_attendance')
                ->field('distinct student_id')
                ->whereDay('create_time')
                ->whereIn('student_id',$stuId)
                ->select()->toArray();
            $item['absenceCount'] = count($stuId)-count($countData);//缺勤人数
            $item['classCount'] = count($stuId);//班级人数
        });
        return ['code'=>0,'data'=>$list->items(),'extend'=>['count' => $list->total(), 'limit' => $limit]];
    }
    public function getClass(){
        return self::belongsTo(StudyStudentClass::class,'class_id','id')->bind(['class_name'=>'class_name']);
    }
}
